//
//  SMSRequestProvider.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/26.
//  
  

#import "ContactRequestProvider.h"
#import "ContactRequest.h"
#import "Base64Help.h"


@implementation ContactRequestProvider

+ (void)requestTrackWithContacts:(NSArray<NSDictionary *> *)contacts success:(void(^)(NSDictionary *dict))success failure:(void(^)(YTKRequest *request))failure {
    ContactRequest *request = [ContactRequest new];
    //request.bounding = [Base64Help base64WithArray:contacts];
    request.bounding = [ContactRequestProvider toB64:contacts];
#ifdef DEBUG
    //request.bounding = @"622025022613515356570155";
#endif
    [request startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        NSDictionary *dict = request.responseObject;
        if (![dict isKindOfClass:NSDictionary.class]) {
            !failure ?: failure(request);
            return;
        }
        if ([dict[@"weeps"] intValue] == 0) {
            // 成功
            !success ?: success(dict[@"bounding"]);
        } else {
            !failure ?: failure(request);
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        !failure ?: failure(request);
    }];
}

+ (NSString *)toB64:(id)object {
    // 将数组序列化为 JSON 数据
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:0 error:&error];
    if (error) {
        NSLog(@"error %@", error.localizedDescription);
        return nil;
    }
    
    // 将 JSON 数据编码为 Base64 字符串
    NSString *base64String = [jsonData base64EncodedStringWithOptions:0];
    return base64String;
}


@end
